package com.hehao.java_basic.proxy.cglib.dynamic;


import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;

/**
 * @author Damon
 * @date 2022/10/26
 */
public class CglibProxyFactory {
    public static Object getProxy(Class<?> clazz, MethodInterceptor methodInterceptor) {
        //创建动态代理增强类
        Enhancer enhancer = new Enhancer();
        //设置类加载器
        enhancer.setClassLoader(clazz.getClassLoader());
        //设置被代理类
        enhancer.setSuperclass(clazz);
        //设置拦截器
        enhancer.setCallback(methodInterceptor);
        //创建代理类
        Object o = enhancer.create();
        return o;
    }
}
